
package zhongbo.java.air.dictionary.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.service.IAirCraftService;
import zhongbo.java.air.dictionary.web.dto.AirCraftDto;
import zhongbo.java.air.dictionary.web.vo.ApiResult;
import java.util.List;

@Api(tags = "飞机信息管理")
@RestController
@RequestMapping("/api/aircraft")
public class AirCraftController {

    @Autowired
    private IAirCraftService airCraftService;

    @ApiOperation("创建飞机信息")
    @PostMapping
    public ApiResult<AirCraft> create(@Validated @RequestBody AirCraftDto dto) {
        AirCraft airCraft = new AirCraft();
        BeanUtils.copyProperties(dto, airCraft);
        airCraftService.save(airCraft);
        return ApiResult.success(airCraft);
    }

    @ApiOperation("更新飞机信息")
    @PutMapping
    public ApiResult<AirCraft> update(@Validated @RequestBody AirCraftDto dto) {
        if (dto.getId() == null) {
            return ApiResult.validateFailed("更新时ID不能为空");
        }
        AirCraft airCraft = new AirCraft();
        BeanUtils.copyProperties(dto, airCraft);
        airCraftService.updateById(airCraft);
        return ApiResult.success(airCraft);
    }

    @ApiOperation("删除飞机信息")
    @DeleteMapping("/{id}")
    public ApiResult<Void> delete(@PathVariable String id) {
        airCraftService.removeById(id);
        return ApiResult.success();
    }

    @ApiOperation("获取飞机信息")
    @GetMapping("/{id}")
    public ApiResult<AirCraft> getById(@PathVariable String id) {
        return ApiResult.success(airCraftService.getById(id));
    }

    @ApiOperation("根据ICAO码获取飞机信息")
    @GetMapping("/icao/{icaoCode}")
    public ApiResult<AirCraft> getByIcaoCode(@PathVariable String icaoCode) {
        return ApiResult.success(airCraftService.getByIcaoCode(icaoCode));
    }

    @ApiOperation("分页查询飞机信息")
    @GetMapping("/page")
    public ApiResult<IPage<AirCraft>> page(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long current,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long size,
            @ApiParam("排序字段") @RequestParam(required = false) String orderField,
            @ApiParam("是否升序") @RequestParam(defaultValue = "false") boolean isAsc) {
        IPage<AirCraft> page = new Page<>(current, size);
        return ApiResult.success(airCraftService.pageWithOrder(page, orderField, isAsc));
    }

    @ApiOperation("根据航空公司查询飞机列表")
    @GetMapping("/airline/{airline}")
    public ApiResult<List<AirCraft>> listByAirline(@PathVariable String airline) {
        return ApiResult.success(airCraftService.listByAirline(airline));
    }

    @ApiOperation("根据状态查询飞机列表")
    @GetMapping("/status/{status}")
    public ApiResult<List<AirCraft>> listByStatus(@PathVariable String status) {
        return ApiResult.success(airCraftService.listByStatus(status));
    }
}
